<?php 
	include '../init.php';
	include ADMIN_PATH.'/header.php';
	//查询数据库
	$model=new Model('order_list');
	//每页多少条
	$num=2;
	//查询总条数
	$sql="select count(*) from `order_list` ol,`user` u,`address` ad where ol.uid=u.id and ol.add_id=ad.id";
	//总条数
	$total=$model->find($sql)[0]['count(*)'];
	//var_dump($total);
	//总页数
	$pages=ceil($total/$num);

	//获取当前页
	$p=$_GET['p']?$_GET['p']:1;
	//尾页处理
	$p=min($p,$pages);
	//首页处理
	$p=max($p,1);
	//上一页
	$prev=$p-1;
	//下一页
	$next=$p+1;

	//每页开始
	$start=($p-1)*$num;
	//每页结束(偏移量)
	$offset=$num;

	$sql1="select
			ol.id 			olid,
			ol.ordernumber  olo,
			ol.is_pay       olpay,
			ol.is_delivery  olde,
			ad.address 		adad,
			ad.receiver		adre,
			ad.tel 			adtel,
			u.username 		uname,
			ol.ordertime 	oltime 
		   from
			order_list      ol,
			user 			u,
			address 		ad
		   where
		   	ol.add_id=ad.id
		   and
		    ol.uid=u.id
		   order by ol.ordertime desc 
		   limit $start,$offset
		  ";
	$list=$model->find($sql1);
	//var_dump($list);
	$str="
		<a href='".ADMIN_URL."/order/index.php'>首页</a>
		<a href='".ADMIN_URL."/order/index.php?p=$prev'>上一页</a>
		<a href='".ADMIN_URL."/order/index.php?p=$p'>$p</a>
		<a href='".ADMIN_URL."/order/index.php?p=$next'>下一页</a>
		<a href='".ADMIN_URL."/order/index.php?p=$pages'>尾页</a>
		";
?>
<div class="m_right">
	<div class="m_main">
		<h1 style="font:bold 26px/40px 宋体;">订单管理</h1>
		<table class="table" border="1" cellspacing="0" cellpadding="5" align="center">
			
			<tr bgcolor="#ccc" height="50">
				<th>编号</th>
				<th>订单编号</th>
				<th>买家</th>
				<th>收货人</th>
				<th>收货地址</th>
				<th>联系电话</th>
				<th>是否付款</th>
				<th>是否收货</th>
				<th>操作</th>
			</tr>
			<?php if (!empty($list)) :?>
				<?php foreach ($list as $key => $value) :?>
					<tr height="50">
						<td><?php echo $value['olid'];?></td>
						<td><?php echo $value['olo'];?></td>
						<td><?php echo $value['uname'];?></td>
						<td><?php echo $value['adre'];?></td>
						<td><?php echo $value['adad'];?></td>
						<td><?php echo $value['adtel'];?></td>
						<td><?php echo $value['olpay']?'<span>已付款</span>':'<span>未付款</span>';?></td>
						<td><?php 
						switch ($value['olde']) {
							case 0:
								echo '<a href="'.ADMIN_URL.'/order/action.php?a=fa&p='.$p.'&id='.$value['olid'].'">发货</a>';
								break;
							
							case 1:
								echo '未收货';
								break;
							case 2:
								echo '已收货';
								break;
						}
						?>
						</td>
						<td><a href="<?php echo ADMIN_URL;?>/order/detail.php?id=<?php echo $value['olid'];?>">查看订单详情</a>|
						<a href="<?php echo ADMIN_URL;?>/order/action.php?a=del&id=<?php echo $value['olid'];?>">取消订单</a>
						</td>
					</tr>
				<?php endforeach;?>
			<?php else :?>
				<tr>
					<th>还木有订单呢！</th>
				</tr>	
			<?php endif;?>
			<tr>
				<th colspan="9" height="50px">
					<?php echo $str;?>
				</th>
			</tr>
		</table>
	</div>
</div>
<?php 
	include ADMIN_PATH.'/footer.html';
?>